/*
 * Copyright 2010, Andrew M Gibson
 *
 * www.andygibson.net
 *
 * This file is part of DataValve.
 *
 * DataValve is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DataValve is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DataValve.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fluttercode.datavalve.provider.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.RowProcessor;

/**
 * 把ResultSet当前行转换为一个Map
 */
public class MapMapper implements ResultSetObjectMapper<Map<String, Object>> {

    /**
     * Singleton processor instance that handlers share to save memory.
     * Notice the default scoping to allow only classes in this package to use
     * this instance.
     */
    static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor();

    /**
     * Convert a <code>ResultSet</code> row into a <code>Map</code>.
     *
     * <p>
     * This implementation returns a <code>Map</code> with case insensitive column names as keys.
     * Calls to <code>map.get("COL")</code> and <code>map.get("col")</code> return the same value.
     * Furthermore this implementation will return an ordered map,
     * that preserves the ordering of the columns in the ResultSet,
     * so that iterating over the entry set of the returned map will return
     * the first column of the ResultSet, then the second and so forth.
     * </p>
     *
     * @param rs ResultSet that supplies the map data
     * @return the newly created Map
     * @throws SQLException if a database access error occurs
     */
    @Override
    public Map<String, Object> createObjectFromResultSet(ResultSet rs) throws SQLException {
        return ROW_PROCESSOR.toMap(rs);
    }
}
